package cn.edu.hust.cm.vs;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.tutorial.AddressBookProtos;
import com.google.gson.Gson;
import com.google.protobuf.nano.CodedOutputByteBufferNano;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class SerializeActivity extends AppCompatActivity {

    private static final String TAG = "SerializeActivity";
    private EditText mEtTimes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serialize);

        mEtTimes = (EditText) findViewById(R.id.et_times);
    }

    private int getOpTimes() {
        try {
            return Integer.parseInt(mEtTimes.getText().toString());
        } catch (NumberFormatException e) {
            Log.e(TAG, "getOpTimes: ", e);
            return 10;
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_json_encode:
                Toast.makeText(this, "Costs " + jsonEncode(getOpTimes()), Toast.LENGTH_SHORT).show();
                break;

            case R.id.btn_gson_encode:
                Toast.makeText(this, "Costs " + gsonEncode(getOpTimes()), Toast.LENGTH_SHORT).show();
                break;

            case R.id.btn_pb_encode:
                Toast.makeText(this, "Costs " + pbEncode(getOpTimes()), Toast.LENGTH_SHORT).show();
                break;

            case R.id.btn_pbn_encode:
                Toast.makeText(this, "Costs " + pbnEncode(getOpTimes()), Toast.LENGTH_SHORT).show();
                break;

            case R.id.btn_pb_decode:
                break;
            case R.id.btn_pbn_decode:
                break;
        }
    }

    private long jsonEncode(int loop) {
        long now = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            com.example.tutorial.nano.AddressBookProtos.Person.PhoneNumber phoneNumber = new com.example.tutorial.nano.AddressBookProtos.Person.PhoneNumber();
            phoneNumber.number = "110";
            phoneNumber.type = com.example.tutorial.nano.AddressBookProtos.Person.HOME;

            com.example.tutorial.nano.AddressBookProtos.Person person = new com.example.tutorial.nano.AddressBookProtos.Person();
            person.name = "Huster";
            person.email = "410087336@163.com";
            person.id = 1000;
            person.phones = new com.example.tutorial.nano.AddressBookProtos.Person.PhoneNumber[]{phoneNumber};

            JSONObject phone = new JSONObject();
            JSONArray phonesArray = new JSONArray();
            JSONObject personObject = new JSONObject();
            try {
                phone.put("number", phoneNumber.number);
                phone.put("type", phoneNumber.type);

                phonesArray.put(phone);

                personObject.put("name", person.name);
                personObject.put("email", person.email);
                personObject.put("id", person.id);
                personObject.put("phones", phonesArray);
                personObject.toString();
                // Log.i(TAG, "jsonEncode: " + personObject.toString());
            } catch (JSONException e) {
                Log.e(TAG, "jsonEncode: ", e);
            }
        }
        return (System.currentTimeMillis() - now);
    }

    private long gsonEncode(int loop) {

        Gson gson = new Gson();
        long now = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            com.example.tutorial.nano.AddressBookProtos.Person.PhoneNumber phoneNumber = new com.example.tutorial.nano.AddressBookProtos.Person.PhoneNumber();
            phoneNumber.number = "110";
            phoneNumber.type = com.example.tutorial.nano.AddressBookProtos.Person.HOME;

            com.example.tutorial.nano.AddressBookProtos.Person person = new com.example.tutorial.nano.AddressBookProtos.Person();
            person.name = "Huster";
            person.email = "410087336@163.com";
            person.id = 1000;
            person.phones = new com.example.tutorial.nano.AddressBookProtos.Person.PhoneNumber[]{phoneNumber};

            gson.toJson(person);
            // Log.i(TAG, "gsonEncode: " + gson.toJson(person));
        }
        return (System.currentTimeMillis() - now);
    }

    private long pbEncode(int loop) {
        long now = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            AddressBookProtos.Person.Builder personBuilder = AddressBookProtos.Person.newBuilder();

            AddressBookProtos.Person.PhoneNumber.Builder phoneNumberBuilder = AddressBookProtos.Person.PhoneNumber.newBuilder();
            phoneNumberBuilder.setNumber("110");
            phoneNumberBuilder.setType(AddressBookProtos.Person.PhoneType.HOME);

            personBuilder.setName("Huster");
            personBuilder.setEmail("410087336@163.com");
            personBuilder.setId(1000);
            personBuilder.addPhones(phoneNumberBuilder.build());

            AddressBookProtos.Person person = personBuilder.build();
            person.toByteArray();
        }
        return (System.currentTimeMillis() - now);
    }

    private long pbnEncode(int loop) {
        long now = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            com.example.tutorial.nano.AddressBookProtos.Person.PhoneNumber phoneNumber = new com.example.tutorial.nano.AddressBookProtos.Person.PhoneNumber();
            phoneNumber.number = "110";
            phoneNumber.type = com.example.tutorial.nano.AddressBookProtos.Person.HOME;

            com.example.tutorial.nano.AddressBookProtos.Person person = new com.example.tutorial.nano.AddressBookProtos.Person();
            person.name = "Huster";
            person.email = "410087336@163.com";
            person.id = 1000;
            person.phones = new com.example.tutorial.nano.AddressBookProtos.Person.PhoneNumber[]{phoneNumber};

            try {
                person.writeTo(CodedOutputByteBufferNano.newInstance(new byte[person.getSerializedSize()]));
            } catch (IOException e) {
                Log.e(TAG, "pbnEncode: ", e);
            }
        }
        return (System.currentTimeMillis() - now);
    }
}
